package com.wx.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.wx.dao.NeedDao;
import com.wx.model.Need;
import com.wx.util.DBUtil;

public class NeedDaoImpl implements NeedDao {

	@Override
	public List<Need> selectAllNeed(int pageNum, int pageSize) throws Exception {
		List<Need> list = new ArrayList<>();
		Connection connection = DBUtil.getConnection();
		PreparedStatement ps = connection.prepareStatement("select * from need limit ?,?;");
		ps.setInt(1, pageNum);
		ps.setInt(2, pageSize);
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			Need need = new Need(rs.getInt(1), rs.getInt(2), rs.getString(3),rs.getString(4) ,rs.getDate(5), rs.getInt(6));
			list.add(need);
		}
		return list;
	}

	@Override
	public int selectNeedCount() throws Exception {
		int sum = 0;
		Connection connection = DBUtil.getConnection();
		PreparedStatement ps = connection.prepareStatement("select count(*) from need ;");
		ResultSet rs = ps.executeQuery();
		if(rs.next()){
			sum= rs.getInt(1);
		}
		return sum;
	}

}
